<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    @dialog-close="dialogClose"
    dialog-width="600px"
  >
    <template #searchPanel>
      <search-panel
        :addPer="'SystemDict:create'"
        :selectPer="'SystemDict:page'"
        addTitle="新增险种"
        @insert="handleDataDialog({}, '新增险种')"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <el-input
          v-model="searchOptions.category"
          clearable
          placeholder="请输入类别"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="编辑"
            style="cursor: pointer"
            v-permission="'SystemDict:update'"
            @click="handleDataDialog(row, '编辑险种')"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'SystemDict:delete'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
    <template #dialog>
      <dialog-data-form
        :dialogData="selectItemInfo"
        @handleDataOperate="handleDataOperate"
        @dialog-close="dialogClose"
      />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import DialogDataForm from './components/DialogDataForm.vue'
import { 
  insuranceTypeList, 
  updateInsuranceType, 
  createInsuranceType, 
  deleteInsuranceType,
  insuranceTypeStatus 
} from '@/api/after-sale/systemManage/insurance'

export default {
  components: {
    DialogDataForm,
  },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: insuranceTypeList,
      dataName: ['content', 'totalRecords'],
      addData: createInsuranceType,
      updateData: updateInsuranceType,
      deleteData: deleteInsuranceType,
      format(res) {
        return res.map(data => {
          data.statusText = data.status ? '启用' : '禁用'
          return data
        })
      },
    }),
  ],
  data () {
    return {
      tableColumnAttributes: [
        {
          name: '保险分公司名称',
          value: 'companyName',
          type: 'text',
        },
        {
          name: '类别',
          value: 'category',
          type: 'text',
        },
        {
          name: '状态',
          value: 'status',
          type: 'render',
          valueId: 'status',
          value: 'statusText',
          type: 'switch', 
          switchPer: '114', 
          action: this.updateState,
          width: '150'
        },
        {
          name: '说明',
          value: 'remark',
          type: 'text',
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'text',
        },
      ],
      selectItemInfo: {},
    }
  },
  created () { },
  methods: {
    handleDataDialog(row, type) {
      this.selectItemInfo = row;
      this.handleDataInsert(type)
    },
    // 查询
    search () {
      this.currentPage = 1;
      this.setTableData()
    },
    //状态更新
    async updateState(row) {
      const { success } = await insuranceTypeStatus(row.id)
      if (success) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.setTableData()
      } else {
        this.$message.error('操作失败')
      }
    }
  },
  mounted () {},
}
</script>
